﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using InfoJetSoft.Service.Entity;
using InfoJetSoft.Service.Util;

//CID 0656 //2010.10.22 //Begin
namespace InfoJetSoft.Service.Worker
{
    class PNGAddPrincipalsWorker
    {
        public static void addPrincipals(Form form)
        {
            string contextId = form.ParamTable["xdoc_param_png_context_id"] as string;
            string pngId = form.ParamTable["xdoc_param_png_id"] as string;
            XmlElement contextElement = DOMUtil.findNodeByJetId(form, contextId) as XmlElement;
            if (contextElement != null)
            {
                string xml = form.ParamTable["xdoc_param_png_added_xml"] as string;
                XmlDocument principalsDoc = new XmlDocument();
                principalsDoc.LoadXml(xml);

                //CID 0733 //2010.12.18 //Begin
                PNGInfo pngInfo = form.Manifest.PNGInfoTable[pngId] as PNGInfo;
                if (pngInfo.AllowMultiple.Equals("false"))
                {
                    contextElement.InnerXml = "";
                }
                //CID 0733 //2010.12.18 //End

                //CID 0914 //2011.05.20 //Begin
                string accountIdFilter = NamespaceUtil.toFullName(pngInfo.Prefix, "Person") + "/" + NamespaceUtil.toFullName(pngInfo.Prefix, "AccountId");
                //CID 0914 //2011.05.20 //End
                ArrayList accoundIdList = new ArrayList();
                XmlNodeList accountIdElementList = DOMUtil.selectNodes(contextElement, accountIdFilter, 0, form.XmlNsResolver);
                foreach (XmlElement accountIdElement in accountIdElementList)
                {
                    string accountId = accountIdElement.InnerText.Trim();
                    if (accountId.Length <= 0)
                    {
                        contextElement.RemoveChild(accountIdElement.ParentNode);
                    }
                    else
                    {
                        accoundIdList.Add(accountId);
                    }
                }

                XmlNodeList principalElementList = principalsDoc.SelectNodes("/Principals/Principal");
                foreach (XmlElement principalElement in principalElementList)
                {
                    string displayName = DOMUtil.getNodeValue(principalElement.SelectSingleNode("DisplayName"));
                    string accountName = DOMUtil.getNodeValue(principalElement.SelectSingleNode("AccountName"));
                  //Modified by lishuchao 2013.7.21
				    string principalType = string.Empty;
                    if (principalElement.SelectSingleNode("PrincipalType") != null)
                        principalType = DOMUtil.getNodeValue(principalElement.SelectSingleNode("PrincipalType"));

                    bool found = false;
                    foreach (string accountId in accoundIdList)
                    {
                        if (accountName.Equals(accountId))
                        {
                            found = true;
                            //CID 0750 //2010.12.30 //Begin
                            break;
                            //CID 0750 //2010.12.30 //End
                        }
                    }

                    if (!found)
                    {
                        //CID 0914 //2011.05.20 //Begin
                        string nsPrefix = pngInfo.Prefix;
                        string nsUri = "http://schemas.microsoft.com/office/infopath/2007/PartnerControls";
                        if (form.Manifest.SolutionFormatVersion.StartsWith("2.0"))
                        {
                            nsUri = form.XmlNsResolver.GetURI(nsPrefix);
                        }
                        XmlElement personElement = form.XmlDoc.CreateElement(nsPrefix, "Person", nsUri);
                        contextElement.AppendChild(personElement);
                        XmlElement displayNameElement = form.XmlDoc.CreateElement(nsPrefix, "DisplayName", nsUri);
                        displayNameElement.InnerText = displayName;
                        personElement.AppendChild(displayNameElement);
                        XmlElement accountIdElement = form.XmlDoc.CreateElement(nsPrefix, "AccountId", nsUri);
                        accountIdElement.InnerText = accountName;
                        personElement.AppendChild(accountIdElement);
                        XmlElement accountTypeElement = form.XmlDoc.CreateElement(nsPrefix, "AccountType", nsUri);
                        //Modified by lishuchao 2013.7.21
						//accountTypeElement.InnerText = DOMUtil.getNodeValue(principalElement.SelectSingleNode("PrincipalType"));
                         accountTypeElement.InnerText = principalType;
						 personElement.AppendChild(accountTypeElement);
                        //CID 0914 //2011.05.20 //End

                        //CID 0828 //2011.03.10 //Begin
                        if (form.FocusItemIndex == -1)
                        {
                            form.FocusItemIndex = DOMUtil.getChildElementIndex(contextElement, personElement);
                        }
                        //CID 0828 //2011.03.10 //End
                    }
                }
            }
        }
    }
}
//CID 0656 //2010.10.23 //End